package com.example.core.queue.distuptor.config;

import com.lmax.disruptor.RingBuffer;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * 维护disruptor队列信息
 */
public class BufferData {

    private static BufferData bufferData=null;


    private ConcurrentMap<String, RingBuffer> concurrentMap=new ConcurrentHashMap<>();

    private BufferData(){}

    public static BufferData init(){
        if(bufferData==null){
            synchronized (BufferData.class){
                if(bufferData==null){
                    bufferData=new BufferData();
                }
            }
        }
        return bufferData;
    }


    public void put(String queueID,RingBuffer buffer){
        if(!concurrentMap.containsKey(queueID)){
            concurrentMap.put(queueID,buffer);
        }
    }

    public void remove(String queueID){
        if(concurrentMap.containsKey(queueID)){
            concurrentMap.remove(queueID);
        }
    }

    public RingBuffer get(String queueID){
        if(concurrentMap.containsKey(queueID)){
            return concurrentMap.get(queueID);
        }
        return null;
    }

    public RingBuffer getDefult(String queueID,RingBuffer ringBuffer){
        if(!concurrentMap.containsKey(queueID)){
            concurrentMap.put(queueID,ringBuffer);
        }
        return concurrentMap.get(queueID);
    }

}
